package pl.edu.agh.ki.ewywiadowka.client.windows.secretary;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.dto.ClassDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.GenericWindow;

public class ModifyClassWindow extends RegisterClassWindow {
	
	private ClassDTO _clas;
	
	public ModifyClassWindow(EWywiadowka wywiadowka, ClassDTO clas) {
		super(wywiadowka);
		
		_annualDateBox.setSelectedIndex(new Date().getYear() - clas.annual.getYear());
		_letterTextBox.setText(clas.letter.toString());
		
		_okButton.setText("Modyfikuj");
	}
	
	private void modifyClass() {
		if (_letterTextBox.getText().length() == 1) {
			Character letter = _letterTextBox.getText().toLowerCase().toCharArray()[0];
			Date date = new Date();
			date.setYear(Integer.valueOf(_annualDateBox.getValue(_annualDateBox.getSelectedIndex())) - 1900);
			date.setMonth(0);
			date.setDate(0);
			ClassDTO clas = new ClassDTO(null, date, letter, _teachers.get(_educatorListBox.getSelectedIndex()));
			
			Const.eWywiadowkaService.modifyClass(clas, new AsyncCallback<Const.Error.ClassModifyMsg>(){

				@Override
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}
				
				@Override
				public void onSuccess(Const.Error.ClassModifyMsg result) {
					Window.alert(result.toString());
				}
				
			});
			
		}
		else 
			Window.alert("Prosze podac litere klasy");
		
	}

	@Override
	public void onClick(ClickEvent event) {
		modifyClass();
	}
	
	

}
